<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="1200px" append-to-body>
        <!-- <div class="layout-route"> -->
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <el-form-item label="商品sku" prop="seller_sku">
                    <el-input v-model.trim="queryParams.seller_sku" placeholder="请输入商品sku" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="产品分类" prop="spu">
                    <el-select v-model="queryParams.product_type" placeholder="请选择产品分类" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in dict.productType" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="热销分类" prop="spu">
                    <el-select v-model="queryParams.hot_type" placeholder="请选择热销分类" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in dict.hotType" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="供应商" prop="supply_id">
                    <SelectPage
                        v-model="queryParams.supply_id"
                        placeholder="请选择供应商"
                        :url="$api.supply.getSupplyList"
                        show_field="supply_name"
                        query_field="supply_name"
                        query_field2="spu"
                        filterable
                        clearable
                        popper-class="custom-select"
                        style="width: 240px"
                    ></SelectPage>
                </el-form-item>
                <el-form-item label="创建时间" prop="create_time">
                    <el-date-picker v-model="queryParams.create_time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">导出</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table v-loading="loading" :data="tableData" border script>
                <el-table-column label="序号" type="index" width="50"></el-table-column>
                <el-table-column label="工厂款号" prop="spu" min-width="60"></el-table-column>
                <el-table-column label="商品名称" prop="product_name" min-width="160"></el-table-column>
                <el-table-column label="产品类型" prop="produce_name" min-width="60">
                    <template slot-scope="scope">
                        {{ scope.row.product_type | formatLabelByValueFuzzy(dict.productType) }}
                    </template>
                </el-table-column>
                <el-table-column label="热销类型" prop="hot_type" min-width="60">
                    <template slot-scope="scope">
                        {{ scope.row.hot_type | formatLabelByValueFuzzy(dict.hotType) }}
                    </template>
                </el-table-column>
                <el-table-column label="图片" prop="image_url" min-width="90">
                    <template slot-scope="scope">
                        <ImageUploadImg v-model="scope.row.image_url" :readOnly="true" class="small-img"></ImageUploadImg>
                    </template>
                </el-table-column>
                <el-table-column label="颜色" prop="color" min-width="60"></el-table-column>
                <el-table-column label="尺寸" prop="size" min-width="60"></el-table-column>
                <el-table-column label="罩杯" prop="cup" min-width="60" v-if="showCupColumn"></el-table-column>
                <el-table-column label="合计" prop="suggest_sku_nums" min-width="60"></el-table-column>
                <el-table-column label="供应商" prop="supply_name" min-width="140"></el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList" />
        </div>
        <!-- </div> -->
    </el-dialog>
</template>

<script>
import { getPlanSuggest, getPurchaserList, exportSkuList } from '@/api/purchase'
import { parseTime } from '@/utils/quickadmin'
import { exportFile } from '@/utils/index'
export default {
    name: 'PurchaseSuggestDialog',
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
    },
    data() {
        return {
            // 遮罩层
            loading: true,
            download: false,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],

            // 状态数据字典
            dict: {
                salesmanList: [],
                productType: [
                    { label: 'bar类型', value: 1 },
                    { label: 'shapewear类型', value: 2 },
                    { label: 'swim类型', value: 3 },
                    { label: 'panties类型', value: 4 },
                    { label: '其他', value: 5 },
                ],
                hotType: [
                    { label: '热销', value: 1 },
                    { label: '潜力款', value: 2 },
                    { label: '新品', value: 3 },
                    { label: '中长尾款', value: 4 },
                ],
            },
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                product_type: undefined,
                seller_sku: undefined,
                hot_type: undefined,
                spu: undefined,
                operate_id: undefined,
                supply_id: undefined,
                create_time: [],
                inventory_quantity: undefined,
                on_way_quantity: undefined,
                not_send_quantity: undefined,
                sku_1days: undefined,
                suggest_sku_nums: undefined,
            },
        }
    },
    watch: {
        params: {
            handler(val) {
                if (val.spu) {
                    this.getList()
                }
            },
            immediate: true,
        },
    },
    computed: {
        title() {
            return `备货建议明细`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
        showCupColumn() { 
            return this.tableData.findIndex(item=>!!item.cup) !== -1
        },
    },
    async created() {
        this.loading = true
        try {
            const salesmanData = await getPurchaserList()
            this.dict.salesmanList = salesmanData?.data ?? []
        } catch (error) {
            this.loading = false
        }
    },

    methods: {
        cancel() {
            this.$emit('cancel')
        },
        handleExport() {
            this.download = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            exportSkuList(qyparams)
                .then(res => {
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    console.log(err)
                    this.download = false
                })
        },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
                spu: this.params.spu,
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            getPlanSuggest(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },
        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },
        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
    },
}
</script>

<style>
.form-class.el-form .el-form-item {
    width: 100%;
    margin-right: 0;
}
.form-class.el-form .el-form-item .el-input {
    width: 380px;
}
</style>
